package team.kirohuji.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import team.kirohuji.domain.SystemUser;
import team.kirohuji.service.SystemUserService;
import team.kirohuji.utils.R;
import team.kirohuji.utils.ShiroUtils;


/**
 * 
 * 这个Controller用来管理用户的登录和查看用户的登录信息
 * 
 * @author kirohuji
 * @version 0.1
 */
@Controller
public class LoginController {
	 private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
	/**
	 * @see SystemUserService
	 */
	@Resource
	private SystemUserService systemUserService;

	/**
	 * 登录
	 * 
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @param session
	 *            会话Session
	 * @return {@link team.kirohuji.utils.R}
	 * @throws IOException
	 *             可能会抛出了一个IO异常
	 */
	@ResponseBody
	@RequestMapping(value = "/sign/in", method = RequestMethod.POST)
	public R login(String username, String password, HttpSession session) throws IOException {
		try {
			Subject subject = ShiroUtils.getSubject();
			UsernamePasswordToken token = new UsernamePasswordToken(username, password);
			subject.login(token);
			session.setAttribute("login_name", username);
			logger.info(username+"成功登录");
		} catch (UnknownAccountException e) {
			return R.error(e.getMessage());
		} catch (IncorrectCredentialsException e) {
			return R.error(e.getMessage());
		} catch (LockedAccountException e) {
			return R.error(e.getMessage());
		} catch (ExcessiveAttemptsException e) {
			return R.error(e.getMessage());
		} catch (AuthenticationException e) {
			return R.error("账户验证失败");
		}

		return R.ok();
	}

	/**
	 * 退出
	 * 
	 * @param session
	 *            Http会话
	 * @return String 进行地址跳转
	 */
	@RequestMapping(value = "/sign/out", method = RequestMethod.GET)
	public String logout(HttpSession session) {
		ShiroUtils.logout();
		logger.info(ShiroUtils.getSessionAttribute("login_name")+"成功退出");
		return "redirect:login.html";
	}

	/**
	 * 获取登录的用户信息
	 * 
	 * @param session
	 *            会话
	 * @return {@link team.kirohuji.utils.R}
	 */
	@RequestMapping("/info")
	@ResponseBody
	public R info(HttpSession session) {
		SystemUser sysUser = (SystemUser) session.getAttribute("user");
		return R.ok().put("user", sysUser);
	}

	/**
	 * 注册的路由映射
	 * 
	 * @return String 地址
	 */
	@RequestMapping(value = "/sign/up", method = RequestMethod.GET)
	public String register() {
		return "page/register";
	}
	
	/**
	 * 进行 路由 设置
	 * @param url
	 * @return
	 */
	@RequestMapping("{suffix}/{url}.html")
	public String router(@PathVariable("suffix") String suffix,@PathVariable("url") String url){
		System.out.println("--------------------zyd-----------------");
		return suffix+"/" + url + ".html";
	}
	
}
